<?php

namespace addons\qingdongams\model;

use think\Model;
use traits\model\SoftDelete;

/**
 *工作报告
 */
class WorkReport extends Model
{
    use SoftDelete;

    protected $name = 'qingdongams_work_report';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [];

    public function getCreatetimeAttr($value)
    {
        return date('Y-m-d H:i', $value);
    }

    //
    public function staff()
    {
        return $this->hasOne(Staff::class, 'id', 'staff_id')
            ->field('id,name,img,department_id');
    }
    //评论
    public function comments()
    {
        return $this->hasMany(Comment::class, 'relation_id', 'id')->where(['relation_type'=>'workreport'])->with('staff')->field('id,relation_id,content,staff_id,file_ids,createtime');
    }

    //明日日程
    public function getTomorrowEventAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        $titles = Event::where(['id' => ['in', $value['id_list'] ?? []]])->column('title');
        $value['titles'] = implode(',', $titles ?? []);
        return $value;
    }

    //客户
    public function getCustomerAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //拜访
    public function getVisitAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //今日日程
    public function getEventAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //订单
    public function getContractAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //工单
    public function getWorkorderAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //回款
    public function getReceivablesAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //完成工单
    public function getCompleteWorkorderAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //未完成日程
    public function getNotEventAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //完成日程
    public function getCompleteEventAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //跟进客户
    public function getFollowUpAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //
    public function getGoodWorkorderAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }

    //
    public function getNoEventAttr($value)
    {
        if (empty($value)) {
            return '';
        }
        $value = json_decode($value, true);

        return $value;
    }
}
